﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace mamnontuoitho7.AppCode
{
    public class Product
    {
        public ConnectGetData _connectData;
        public Product()
        {
            _connectData = new ConnectGetData();
        }

        /// <summary>
        /// create product
        /// </summary>
        /// <param name="productTypeID"></param>
        /// <param name="memberID"></param>
        /// <param name="title"></param>
        /// <param name="introduce"></param>
        /// <param name="productStatusID"></param>
        /// <param name="viewNumber"></param>
        /// <param name="postUser"></param>
        /// <param name="editUser"></param>
        /// <returns></returns>
        public bool ProductCreate(int productTypeID, int memberID, String title, String introduce, int productStatusID, String viewNumber, String postUser, String editUser)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sqlUp = "Insert into Product(ProductTypeID, MemberID, Title, Introduce, ProductStatusID, ViewNumber, PostUser, EditUser) values(@ProductTypeID, @MemberID, @Title, @Introduce, @ProductStatusID, @ViewNumber, @PostUser, @EditUser)";
                SqlCommand cmd = new SqlCommand(sqlUp, _connectData.GetConnect());
                cmd.Parameters.AddWithValue("@ProductTypeID", productTypeID);
                cmd.Parameters.AddWithValue("@MemberID", memberID);
                cmd.Parameters.AddWithValue("@Title", title);
                cmd.Parameters.AddWithValue("@Introduce", introduce);
                cmd.Parameters.AddWithValue("@ProductStatusID", productStatusID);
                cmd.Parameters.AddWithValue("@ViewNumber", viewNumber);
                cmd.Parameters.AddWithValue("@PostUser", postUser);
                cmd.Parameters.AddWithValue("@EditUser", editUser);

                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {

                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }

        /// <summary>
        /// get maximum productID in tb Product
        /// </summary>
        /// <returns></returns>
        public int GetTheMaxProductID()
        {
            string sql = "select MAX(ProductID) as maxID from  Product";

            DataTable dt = _connectData.GetDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                return Convert.ToInt32(dt.Rows[0]["maxID"]);

            }
            else { return 0; }
        }


        /// <summary>
        /// update product status : enable or disable
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="status"></param>
        public void ChangeProductStatus(String productID, String status)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            string sql = "update Product set ProductStatusID = @Status where ProductID = @ProductID";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@ProductID", productID);
            cmd.Parameters.AddWithValue("@Status", status);

            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
        }

        /// <summary>
        /// delete product by ID 
        /// </summary>
        /// <param name="ProductID"></param>
        /// <returns></returns>
        public bool ProductDelete(String productID)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sql = "delete from Product  where ProductID = @ProductID";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@ProductID", productID);
                
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }

        /// <summary>
        /// increase the view in product table
        /// </summary>
        /// <param name="ProductID"></param>
        public void IncreaseProductView(String ProductID)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                SqlCommand command = new SqlCommand("sp_IncreaseProductViewNumber", con);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ProductID", SqlDbType.NVarChar).Value = ProductID;

                command.ExecuteNonQuery();
                con.Dispose();
                con.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine("SQL Error" + ex.Message.ToString());
            }
        }

    }
}